﻿Ext.define('app.system.dic.Index', {
    extend: 'Ext.panel.Panel',
    border: false,
    autoScroll: false,
    initComponent: function () {
        var me = this;
        this.items = [this.getTree(), this.getGrid()];
        me.callParent();
    }
    ,
    layout: 'border',
    getGrid: function () {
        if (!this.grid) {
            var main = this;
            this.grid = new Ext.grid.Panel(
            {
                border: false,
                title: '参数列表',
                region: 'center',
                stateful: true,
                stripeRows: true,
                tbar: [
                    { text: '初始化', handler: function () { main.initData(); } },
                    '->',
                    { text: '新增参数', handler: function () { main.addDict(); } },
                { text: '保存', handler: function () { main.saveUpdate(); } }
                ],
                viewConfig:
                {
                    forceFit: true,
                    enableRowBody: true
                },
                plugins: {
                    ptype: 'cellediting',
                    clicksToEdit: 1
                },
                columns: [
                 { header: "参数名称", width: 300, dataIndex: 'parameterName', editor: { xtype: 'textfield' } },
                 { header: "参数值", dataIndex: 'parameterValue', editor: { xtype: 'textfield' } }
                ],
                store: new Ext.data.JsonStore(
                {
                    autoLoad: false,
                    proxy: {
                        type: 'ajax',
                        url: '/api/services/app/SystemParams/GetParams',
                        reader: {
                            type: 'json',
                            rootProperty: function (node) {
                                return node.result || node.children;
                            }
                        }
                    },
                    fields: ['id', 'Catalog', 'parameterName', 'parameterValue']
                })
            });
        }
        return this.grid;
    },
    addDict: function () {
        var catalog = '';
        var main = this;
        var selectedNodes = this.getTree().getSelectionModel().selected.items;
        if (selectedNodes.length > 0) {
            var node = selectedNodes[0];
            if (node.get('leaf')) {
                catalog = node.get('text');
            }
        }
        var m = Ext.create('app.system.dic.Add', {
            catalog: catalog,
            listeners: {
                'savesuccess': function () {
                    if (catalog) {
                        main.getGrid().getStore().load({ params: { catalog: catalog } });
                    }
                    this.close();
                }
            }
        });
        m.show();
    },
    saveUpdate: function () {
        var store = this.getGrid().getStore();
        var _grid = store.getModifiedRecords();
        if (_grid.length == 0) {
            return;
        }
        var json = [];

        for (var i = 0; i < _grid.length; i++) {
            var id = _grid[i].data.id;
            var val = _grid[i].data.parameterValue;
            var name = _grid[i].data.parameterName;

            json.push({ Id: id, CodeValue: val, DisplayName: name });
        }
        App.AjaxPost('/api/services/app/SystemParams/ParamsUpdate', json, function (data) {
            Ext.Msg.alert("系统提示", "保存成功")
            store.load();
        });
    }, initData: function () {
       
        App.AjaxPost('/api/services/app/SystemParams/Init', null, function (data) {
            Ext.Msg.alert("系统提示", "保存成功")
        });
    },
    getTree: function () {
        var main = this;
        if (!this.tree) {
            this.tree = new Ext.tree.Panel(
            {
                root: { id: 0, text: '参数项', leaf: false },
                rootVisible: true,
                border: false,
                title: '参数分类',
                split: true,
                region: 'west',
                width: 250,
                enableTabScroll: true,
                autoScroll: true,
                animate: true,
                store: new Ext.data.TreeStore({
                    root: {
                        expanded: true,
                        children: []
                    }
                }),
                listeners:
                {
                    'afterrender': function (me) {
                        Ext.Ajax.request({
                            url: '/api/services/app/SystemParams/GetCatalog',
                            scope: this,
                            success: function (response) {

                                console.log('load Data');
                                var menus = Ext.decode(response.responseText);
                                var root = me.getStore().getRootNode();
                                for (var i in menus.result) {
                                    var menugroup = menus.result[i];
                                    root.appendChild({
                                        text: menugroup.text,
                                        leaf: true
                                    });
                                }
                                me.expandAll();
                            }
                        });
                    },
                    'cellclick': function (tree, td, cellIndex, record, tr, rowIndex, e, eOpts) {
                        // console.log(record);
                        if (record.get('leaf')) {
                            //store.proxy.extraParams = filter;
                            main.getGrid().getStore().proxy.extraParams = { catalog: record.data.text };
                            main.getGrid().getStore().load();
                        }

                    }
                }
            });
        }
        return this.tree;
    }
});
